﻿/*
 *Copyright (c) 2014, Riccardo Cagnasso
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */

using System;
using System.Linq;
using System.Security.Cryptography;

namespace PasslibNet.Utils
{
    /// <summary>
    /// This is a cryptographic random string generator.
    /// This uses RNGCryptoServiceProvider 
    /// (http://msdn.microsoft.com/it-it/library/system.security.cryptography.rngcryptoserviceprovider%28v=vs.110%29.aspx)
    /// to generate cryptographic-safe random numbers and then generate strings from them
    /// </summary>
    public class RandomGenerator
    {
        private static readonly string AllChars = "";
        private static readonly RNGCryptoServiceProvider Random;

        static RandomGenerator()
        {
            for (var c = (char)0; c <= (char)255; c++)
            {
                if (!Char.IsControl(c))
                {
                    AllChars += c;
                }
            }

            Random = new RNGCryptoServiceProvider();
        }

        /// <summary>
        /// Generate a random string
        /// 
        /// This is probably somewhat slow
        /// </summary>
        /// <param name="len">The length of the generated string</param>
        /// <returns>A random string composed of len characters chosen between all ASCII non-control characters</returns>
        public static string GenerateString(int len = 16)
        {
            /*
             *This is here for test porpouses, i'll do it better 
             */
            var b = new byte[4];
            RandomGenerator.Random.GetBytes(b);
            var random = new Random(BitConverter.ToInt32(b, 0));
            return new string(Enumerable.Repeat(RandomGenerator.AllChars, len)
                          .Select(s => s[random.Next(s.Length)])
                          .ToArray());
        }

        /// <summary>
        /// Generates n random bytes
        /// </summary>
        /// <param name="count">The number of bytes to generate</param>
        /// <returns>A byte array with count random bytes in it</returns>
        public static Byte[] GenerateBytes(int count)
        {
            var b = new Byte[count];
            RandomGenerator.Random.GetBytes(b);

            return b;
        }

        /// <summary>
        /// Generates a base64 string from a number of random bytes
        /// </summary>
        /// <param name="bytelen">The number of random bytes</param>
        /// <returns>A random base64 string reppresenting bytelen random bytes</returns>
        public static string GenerateBase64String(int bytelen = 8)
        {
            var b = RandomGenerator.GenerateBytes(bytelen);

            return Convert.ToBase64String(b);
        }
    }
}
